$.fn.myTree = function(zNodes, zObj){
    var treeId = this.attr('id')
    var setting = {
        isSimpleData : true,
        treeNodeKey : "id",
        treeNodeParentKey : "pId",
        editable: true,
        dragMove: true,
        callback: {
            beforeDrag: function(treeId, treeNode) {
                if(treeNode.id=='0') return false;
            },
            beforeDrop: function(treeId, treeNode, targetNode, moveType) {
                if(targetNode.id=='0') return false;
                $.getJSON('/admin/'+zObj+'/move', {id: treeNode.id, target_id: targetNode.id, move_type: moveType}, function(result){
                    if(result.flag==0){
                        alert('error!');
                        return false;
                    }
                })
            },
            beforeRemove: function(treeId, treeNode){
                if (treeNode && treeNode.nodes && treeNode.nodes.length > 0) {
                    var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
                    return confirm(msg)
                }
            },
            remove: function(event, treeId, treeNode) {
                $.getJSON('/admin/'+zObj+'/remove', {id: treeNode.id}, function(result){
                    if(result.flag==0){
                        alert('error!');
                        return false;
                    }
                })
            },
            confirmRename: function(treeId, treeNode, newName) {
                if(treeNode.id=='0') return false
                $.getJSON('/admin/'+zObj+'/edit', {id: treeNode.id, name: newName}, function(result){
                    if(result.flag==0){
                        alert('error!');
                        return false;
                    }
                })
            }
        }
    };
    var zTree = $("#"+treeId).zTree(setting, zNodes);
    this.zTree = zTree;
    this.zTreeAdd = function(){
        if(zTree.getSelectedNode()!=null){
            $.getJSON('/admin/'+zObj+'/add', {parent_id: zTree.getSelectedNode().id}, function(result){
                if(result.flag==1){
                    var obj = result.obj
                    zTree.addNodes(zTree.getSelectedNode(), [{id:obj.id, name:obj.name, pId:zTree.getSelectedNode().id, open: true}]);
                }else{
                    alert('error!')
                }
            })
        }
    }
    this.zTreeExpand = function(bool){
        if(bool){
            zTree.expandAll(true)
        }else{
            zTree.expandAll(false)
            zTree.expandNode(zTree.getNodes()[0], true)
        }
    }
    this.setBranch = function(branch, zTree2){
        if(zTree.getSelectedNode()!=null){
            $.getJSON('/admin/'+zObj+'/set_branch', {branch: branch, id: zTree.getSelectedNode().id}, function(result){
                if(result.flag==1){
                    if(result.obj!=undefined){
                        var obj = result.obj;
                        zTree2.cancelSelectedNode()
                        zTree2.addNodes(zTree2.getNodesByParam('level', 0)[0], [{id:obj.id, name:obj.name, pId:0, open: true}]);
                    }
                }else{
                    alert('error!');
                }
            })
        }
    }
    return this;
}
$.fn.myBranch = function(zNodes, zObj, branch, _setting){
    var treeId = this.attr('id')
    var setting = {
        isSimpleData : true,
        treeNodeKey : "id",
        treeNodeParentKey : "pId",
        editable: true,
        dragMove: true,
        callback: {
            beforeDrag: function(treeId, treeNode) {
                return treeNode.id=='0' ? false : true;
            },
            beforeDrop: function(treeId, treeNode, targetNode, moveType) {
                if(targetNode.id=='0' || moveType == 'inner') return false;
                $.getJSON('/admin/'+zObj+'/move_branch', {id: treeNode.id, target_id: targetNode.id, move_type: moveType, branch: branch}, function(result){
                    if(result.flag==0){
                        alert('error!')
                        return false;
                    }
                })
            },
            remove: function(event, treeId, treeNode) {
                $.getJSON('/admin/'+zObj+'/remove_branch', {id: treeNode.id, branch: branch}, function(result){
                    if(result.flag==0){
                        alert('error!');
                        return false;
                    }
                })
            },
            confirmRename: function(treeId, treeNode, newName) {
                if(treeNode.id=='0') return false
                $.getJSON('/admin/'+zObj+'/edit_branch', {id: treeNode.id, name: newName, branch: branch}, function(result){
                    if(result.flag==0){
                        alert('error!');
                        return false;
                    }
                })
            }
        }
    };
    for(var i in _setting){
        setting[i] = _setting[i]
    }
    var zTree = $("#"+treeId).zTree(setting, zNodes);
    this.zTree = zTree;
    this.addBranch = function(){
        $.getJSON('/admin/'+zObj+'/add_branch', {branch: branch}, function(result){
            if(result.flag==1){
                var obj = result.obj;
                zTree.addNodes(zTree.getNodesByParam('level', 0)[0], [{id:obj.id, name:obj.name, pId:0, open: true}]);
            }else{
                alert('error!');
            }
        })
    }
    this.cancelBranch = function(){
        var node = zTree.getSelectedNode();
        if(node!=null){
            $.getJSON('/admin/'+zObj+'/cancel_branch', {id: node.id, branch: branch}, function(result){
                if(result.flag==1){
                    zTree.removeNode(node);
                }else{
                    alert('error!');
                    return false;
                }
            })
        }
    }
    return this;
}
$.fn.easytabs = function(){
    $("ul.tabs li.tab").click(function() {
        $(' li',$(this).parent()).removeClass("active");
        $(this).addClass('active')
        $(this).parent().next().html($(' > div', this).html())
        $(this).parent().next().children().hide();
        $(this).parent().next().children().fadeIn()
    });
    $('ul.tabs').each(function(i, obj){
        $(' li:last', obj).click();
    })
};
/*
function reloadTree(type, treeId, setting, zNodes) {
    var curType = '0';
    var curLi;
    if (curLi) curLi.removeClass("focus");
    if (!type) type = (curType=='0') ? '1' : curType;
    if (type=="1") {
        setting.showLine = true;
        curLi = $("#defaultStyle");
    } else if (type=="2") {
        setting.showLine = false;
        curLi = $("#noLineStyle");
    } else if (type=="3") {
        setting.showLine = true;
        zNodes[0].iconOpen = "demoStyle/img/phone.gif";
        zNodes[0].iconClose = "demoStyle/img/people.gif";
        zNodes[2].icon = "demoStyle/img/home.gif";
        zNodes[1].nodes[0].icon = "demoStyle/img/hardware.gif";
        zNodes[1].nodes[2].icon = "demoStyle/img/people.gif";
        curLi = $("#diyIconStyle");
    } else if (type=="5") {
        setting.showIcon = false;
        curLi = $("#noIconStyle");}

    curLi.addClass("focus");
    curType = type;
    return $("#"+treeId).zTree(setting, zNodes);
}
*/